var db = require("./db.js");
var User = require("../models/User.js");
var sync = require("synchronize");

//sync(User, "find");

function auth(loginid, pass, session) {
    console.log("security::login  loginid:[" + loginid + "] pass:[" + pass + "]");

    //get userdata
    var found = false;
    //sync.fiber(function (){
      User.find({id:loginid, password:pass}, function(e, docs) {
        if (docs.length == 1) {
          console.log("user found.");
          session.auth = true;
          found = true;
        } else {
          console.log("user not found!!!");
          found = false;
        }
console.log("444");
        //return found;
      });
return false;
console.log(found);
    //});
}

function isAuthenticated(session) {

    if ( (typeof session.auth !== "undefined") &&  ("auth" in session) ) {
console.log("authenticated.");
        return true;
    } else {
console.log("not authenticated!");
        return false;
    }

}

function logout(session) {
  delete session.auth;
}

module.exports = {
  auth: auth,
  isAuthenticated: isAuthenticated,
  logout: logout
}
